package com.dsteiner.widgets.client;


import com.dsteiner.widgets.client.NineBoxWidget.NineBoxModel;
import com.dsteiner.widgets.client.RoundBorder.RoundBorderModel;
import com.dsteiner.widgets.client.Widgets.FlowPanelExt;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.ImageExt;
import com.dsteiner.widgets.client.Widgets.InlineHTMLExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.Widgets.SimplePanelExt;
import com.dsteiner.widgets.client.controller.PopupOverlayController;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.dsteiner.widgets.client.resources.NineBoxCssResource;
import com.dsteiner.widgets.client.resources.NineBoxResources;
import com.dsteiner.widgets.client.resources.RoundBorderCssResource;
import com.dsteiner.widgets.client.resources.RoundBorderResources;
import com.dsteiner.widgets.client.resources.WidgetsImageBundle;
import com.dsteiner.widgets.client.resources.WidgetsResources;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class PopupOverlayWidget extends LazyWidget {

	private PopupOverlayController popupOverlayController = new PopupOverlayController();
	private final PopupOverlayModel popupOverlayModel;

	public PopupOverlayWidget(PopupOverlayModel popupOverlayModel) {
		this.popupOverlayModel = popupOverlayModel;
		popupOverlayController.registerPopupOverlayModel(popupOverlayModel);
		popupOverlayController.registerPopupOverlayWidget(this);
	}

	@Override
	public Widget createContentWidget() {
		FlowPanelExt mainPanel = new FlowPanelExt();
		ImageExt closeIcon = new ImageExt(WidgetsImageBundle.Util.getInstance().closeIcon().createImage());
		SimplePanelExt contentPanel = new SimplePanelExt();
		RoundBorderCssResource css = RoundBorderResources.Util.INSTANCE.css();
		RoundBorderModel roundBorderModel = new RoundBorderModel();
		roundBorderModel.setContentWidget(mainPanel);
		roundBorderModel.setCssResource(css);
		RoundBorder decoratorPanel = new RoundBorder(roundBorderModel);
		InlineHTMLExt closeLink = new InlineHTMLExt("<a href=\"javascript:void(0);\">cerrar</a>");
		
		mainPanel.add((Widget)closeIcon);
		mainPanel.add((Widget)contentPanel);
		FlowPanel bottomPanel = new FlowPanel();
		bottomPanel.add(closeLink);
		mainPanel.add(bottomPanel);
		
		setStyleName(WidgetsResources.INSTANCE.css().popupOverlayWidget());
		closeIcon.addStyleName(WidgetsResources.INSTANCE.css().closeIcon());
		closeLink.addStyleName(WidgetsResources.INSTANCE.css().closeLink());
		contentPanel.addStyleName(WidgetsResources.INSTANCE.css().contentPanel());
		bottomPanel.getElement().getStyle().setProperty("textAlign", "right");
		
		popupOverlayController.registerJsUtil(JsUtil.getInstance());
		popupOverlayController.registerEventManager(EventManagerImpl.getInstance());
		popupOverlayController.registerMainPanel(mainPanel);
		popupOverlayController.registerContentPanel(contentPanel);
		popupOverlayController.registerCloseIcon(closeIcon);
		popupOverlayController.registerGlassPane(GlassPane.getInstance());
		popupOverlayController.registerCloseLink(closeLink);
		return decoratorPanel;
	}

}